package com.ynny.service.impl;

import com.ynny.dao.BookDao;
import com.ynny.entity.Book;
import com.ynny.service.BookManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BookManagerServiceImpl implements BookManagerService {

    @Autowired
    BookDao bookDao;

    @Transactional
    @Override
    public int checkBook(Integer bookId) {
        //1.1查询图书是否存在
        Book book = bookDao.selectBookById(bookId);
        if (book == null) {
            return -1;//图书不存在
        }
        //1.2获取图书价格
        Integer price = book.getPrice();
        //2.1查询库存是否充足
        Integer stock = book.getStock();
        if (stock < 1) {
            return -2;//库存不足
        }
        //2.2减少图书的库存
        int i = bookDao.updateStock(bookId);
        if (i != 1){
            return -3;//服务器异常 更新库存失败
        }

        return price;
    }
}
